﻿using DataAccess;
using DataAccess.Proxy;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.Manager.Controls
{
    public partial class ctMenuAdmin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getMenu();

            xulyClickEven_Menu();

        }
        private void getMenu()
        {
            var tbuser = Session.GetCurrentUser();
            if (tbuser != null)
            {
                string pass = tbuser.Password;
                string username = tbuser.UserName;
                object[] b = { username, pass };
                vw_GetNumberpermision_Proxy view = new vw_GetNumberpermision_Proxy();
                var listmenu = view.GetViewPermision(b);
                var listroot = listmenu.FindAll(x => x.ParentID == 0);
                Queue<MenuItem> queueparent = new Queue<MenuItem>();

                listroot.ForEach(x =>
                {
                    var menuitem = new MenuItem(x.MenuName, x.MenuID.ToString(), "");
                    menuBar.Items.Add(menuitem);
                    queueparent.Enqueue(menuitem);
                });

                while (queueparent.Count() > 0)
                {
                    var menuparent = queueparent.Dequeue();
                    var listchild = listmenu.FindAll(x => x.ParentID.ToString().Equals(menuparent.Value));
                    listchild.ForEach(x =>
                    {
                        var menuitem = new MenuItem(x.MenuName, x.MenuID.ToString(), "");
                        menuparent.ChildItems.Add(menuitem);
                        queueparent.Enqueue(menuitem);
                    });
                }
            }
        }

        public void xulyClickEven_Menu()
        {
            menuBar.MenuItemClick += mnu_MenuItemClick;
        }
        private void mnu_MenuItemClick(object sender, MenuEventArgs e)
        {
            System.Web.UI.WebControls.Menu n = ((System.Web.UI.WebControls.Menu)(sender));
            var menuId = int.Parse(n.SelectedValue);
            var proxy = new tbMenu_Proxy();
            var _menu = proxy.GetById_tbMenu_Proxy(menuId).ToObject<tbMenu>();
            if (_menu != null && _menu.Link != null && !string.IsNullOrWhiteSpace(_menu.Link))
            {
                Response.Redirect(string.Format("~/MainSys/Manager/Admin.aspx?Controlid=" + menuId));
            }
        }
    }
}